package com.itheima.service;

import com.itheima.pojo.Account;

import java.util.ArrayList;
import java.util.Scanner;

public class UserManger {

    ArrayList<Account> li = new ArrayList<>();
    Scanner sc = new Scanner(System.in);

    public void userManagerStart(ArrayList<Account> list) {
        System.out.println("=========用户管理==========");
        while (true) {
            System.out.println("请选择以下功能:");
            System.out.println("\r\t1.用户浏览\t2.权限管理\t3.删除用户\t4.退出");
            String choice = sc.next();
            switch (choice) {
                case "1" -> userInformation(list);
                case "2" -> userPower(list);
                case "3" -> userDelete(list);
                case "4" -> {
                    System.out.println("退出用户管理!");
                    return;
                }
                default -> System.out.println("你输入的信息有误,请重新输入!");
            }
        }
    }

    //用户信息

    public void userInformation(ArrayList<Account> list) {
        for (Account account : list) {
            if ("USER".equals(account.getRole())) {
                System.out.println(account);
            }
        }
    }

    //用户权限
    public void userPower(ArrayList<Account> list) {
        System.out.println("所有非管理员用户名信息如下:");
        for (int i = 0; i < list.size(); i++) {
            if ("USER".equals(list.get(i).getRole())) {
                System.out.println(list.get(i).getUsername());
            }
        }
        System.out.println("禁用名单:");
        for (int i = 0; i < li.size(); i++) {
            System.out.println(li.get(i).getUsername());

        }
        System.out.println("请输入要查询的用户名具体信息:");
        String next = sc.next();
//        System.out.println("已启用的账户:");
        for (int i = 0; i < list.size(); i++) {
            if (next.equals(list.get(i).getUsername())) {
                System.out.println("姓名：" + list.get(i).getName());
                System.out.println("性别：" + list.get(i).getGender());
                System.out.println("年龄：" + list.get(i).getAge());
                System.out.println("生日：" + list.get(i).getBirthday());
                System.out.println("地址：" + list.get(i).getAddress());
                System.out.println("用户名：" + list.get(i).getUsername());
            }
        }
//        System.out.println("已禁用的用户:");
        for (int i = 0; i < li.size(); i++) {
            if (next.equals(li.get(i).getUsername())) {
                System.out.println("姓名：" + li.get(i).getName());
                System.out.println("性别：" + li.get(i).getGender());
                System.out.println("年龄：" + li.get(i).getAge());
                System.out.println("生日：" + li.get(i).getBirthday());
                System.out.println("地址：" + li.get(i).getAddress());
                System.out.println("用户名：" + li.get(i).getUsername());
            }
        }


        System.out.println("请输入要修改密码的账户的用户名:");
        String next1 = sc.next();
        for (int i = 0; i < list.size(); i++) {
            if (next1.equals(list.get(i).getUsername())) {
                System.out.println("请输入修改后的密码");
                String i1 = sc.next();
                list.get(i).setPassword(i1);
                System.out.println("修改成功!");
            }
        }
        for (int i = 0; i < li.size(); i++) {
            if (next1.equals(li.get(i).getUsername())) {
                System.out.println("请输入修改后的密码");
                String i1 = sc.next();
                li.get(i).setPassword(i1);
                System.out.println("修改成功!");
            }
        }


        System.out.println("请输入要禁用或启用的账户:");
        String next2 = sc.next();
        while (true) {
            System.out.println("禁用请输入1,启用输入2:");
            String next3 = sc.next();
            if ("1".equals(next3)) {
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getUsername().equals(next2)) {
                        li.add(list.get(i));
                        list.remove(i);
                        i--;
                    }
                }
                break;
            } else if ("2".equals(next3)) {
                for (int i = 0; i < li.size(); i++) {
                    if (li.get(i).getUsername().equals(next2)) {
                        list.add(li.get(i));
                        li.remove(i);
                        i--;
                    }
                }
                break;
            } else {
                System.out.println("输入信息有误,请重新输入!");
            }
        }

    }


    public void userDelete(ArrayList<Account> list) {
        System.out.println("所有用户名信息如下:");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getUsername());
        }
        System.out.println("禁用名单:");
        for (int i = 0; i < li.size(); i++) {
            System.out.println(li.get(i).getUsername());

        }

        System.out.println("请输入要删除的账户信息");
        String next = sc.next();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUsername().equals(next)) {
                list.remove(i);
                i--;
                System.out.println("删除成功!");
            }
        }
        for (int i = 0; i < li.size(); i++) {
            if (li.get(i).getUsername().equals(next)) {
                li.remove(i);
                i--;
                System.out.println("删除成功!");
            }
        }
    }


}
